<?php
Magnum::import('Magnum_Utility_Stack_Item');
/**
 * stack items manager
 * @uses Magnum_Utility_Stack_Item
 */
class Magnum_Utility_Stack_Manager
{
    /**
     * first of all, a holder of all items
     * @var array
     */
    protected $_stack = array();


    /**
     * constructor...
     */
    public function __construct()
    {
    }// end __construct


    /**
     * set an item into the stack
     * @param $key the key to the item
     * @param $item the item itself
     * @param $type default is overwrite.
     * @return $this
     */
    public function set($key, $item, $type = 'OVERWRITE')
    {
        // if not overwrite...
        if ($type != 'OVERWRITE') {
            return $this->get($key, true)->set($item, null, $type);
        }

        return $this->get($key, true)->remove()->set($item);

    }// end set


    /**
     * append an item by key
     * @param $key the key to the item
     * @param $item the item itself
     * @return $this
     */
    public function append($key, $item)
    {
        $this->get($key, true)->append($item, null, $type);

    }// end append


    /**
     * prepend an item by key
     * @param $key the key to the item
     * @param $item the item itself
     * @return $this
     */
    public function prepend($key, $item)
    {
        $this->get($key, true)->prepend($item, null, $type);

    }// end prepend


    public function clear($key)
    {
        // just remove it...
        if (isset($this->_stack[$key])) {
            unset($this->_stack[$key]);
        }

    }// end clear


    /**
     * get items by $key
     * @param $key the key
     * @param $set if true, set it!
     * @return mixed
     */
    public function get($key, $set = false)
    {
        if (isset($this->_stack[$key])) {
            return $this->_stack[$key];
        }

        if ($set) {
            $this->_stack[$key] = new Magnum_Utility_Stack_Item($key);
            return $this->_stack[$key];
        }

        return false;

    }// end get


    /**
     * get the entire stack
     * @return array
     */
    public function getStack()
    {
        return $this->_stack;

    }// end getStack

}// end Magnum_Utility_Stack_Manager
?>